Page({
  data: {
    dialogVisible: false,
    list: [],
    isFirstLoad: true
  },
  onShow() {
    this.getRoomList()
    isFirstLoad: false
  },
  async getRoomList() {
    const res = await wx.http.get('/room')
    console.log(res);
    this.setData({
      list: res.data
    })
  },
  async swipeClose(ev) {
    const { position, instance } = ev.detail

    if (position === 'right') {
      // vant组件 显示 Dialog 对话框
      // this.setData({
      //   dialogVisible: true,
      // })

      // 微信小程序自带
      const res = await wx.showModal({
        content: '是否删除已绑定房屋？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '删除',
        confirmColor: '#5591af'
      })
      // swiper-cell 滑块关闭
      instance.close()
      console.log(res);
      if (res.confirm) {
        const nRes = await wx.http.delete(`/room/${ev.mark.id}`)
        // console.log(nRes);
        // 前端乐观UI更新
        this.setData({
          list: this.data.list.filter(ele => ele.id !== ev.mark.id)
        })
      }
    }
  },

  goDetail(ev) {
    // console.log(ev);
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
